import json
import glob

data_dir = '/workspace/psycho_trainning/data/raw_data/action_response_data/0709'
files = glob.glob("/workspace/psycho_trainning/data/raw_data/action_response_data/0709/*.json")

stage_action_dict = {}
no_stage_set = set()

for file in files:
    data = json.load(open(file, 'r'))
    session_id = data['name'].replace('.json', '')
    
    dialogs = data['dialog']
    for turn in dialogs:
        if turn['edit'] == False and 'skip' in turn and turn['skip'] == 1:
            continue
        try:
            process = turn['stepFrame']
        except:
            no_stage_set.add(session_id)
        new_action = turn['new_action']
        
        actions_session_id = []
        turn_id = turn['turn_id']
        actions_list = []
        
        for na in new_action:
            actions_session_id.append({'action':na['action_name'], 'session_id': session_id, 'turn_id': turn_id})
            actions_list.append(na['action_name'])
            
        if process not in stage_action_dict:
            stage_action_dict[process] = {
                'action_set': actions_list,
                'action_session_id': actions_session_id,
            }
            
        else:
            for ac in actions_list:
                if ac not in stage_action_dict[process]['action_set']:
                    stage_action_dict[process]['action_set'].append(ac)
            stage_action_dict[process]['action_session_id'].extend(actions_session_id)
        

json.dump(stage_action_dict, open('./stage_action_dict.json', 'w', encoding='utf-8'), ensure_ascii=False)
json.dump(list(no_stage_set), open('./no_process_session_ids.json', 'w', encoding='utf-8'), ensure_ascii=False)
    
    